<template>
  <div class="pageTools">
    <!-- 头部公共组件 -->
    <pageTools>
      <template #left>
        <div>
          <el-tag>
            <i class="el-icon-info" />
            共5条信息
          </el-tag>
        </div>
      </template>
      <template #right>
        <div>
          <el-button size="mini" type="danger">普通excel导出</el-button>
          <el-button size="mini" type="success">普通excel导入</el-button>
          <el-button size="mini" type="primary" @click="addEmpDia">新增员工</el-button>
        </div>
      </template>
    </pageTools>

    <!-- 表格 -->
    <el-card class="el-card">
      <el-table :data="list" border style="width: 100%">
        <el-table-column type="index" :index="index" label="序号" width="60" />
        <el-table-column sortable prop="username" label="姓名" />
        <el-table-column label="头像">
          <template slot-scope="{ row }">
            <img
              v-imgEor="undeatImg"
              style="width: 100px; height: 100px; border-radius: 50%"
              :src="row.staffPhoto || undeatImg"
              alt=""
            >
          </template>
        </el-table-column>
        <el-table-column sortable prop="mobile" label="手机号" />
        <el-table-column sortable label="工号" prop="workNumber" />
        <el-table-column sortable label="聘用形式">
          <template slot-scope="{ row }">{{
            handleEmployment(row.formOfEmployment) || '待定'
          }}</template>
        </el-table-column>
        <el-table-column sortable label="部门" prop="departmentName" />
        <el-table-column sortable label="入职时间" width="170">
          <template slot-scope="{ row }">{{
            parseDate(row.timeOfEntry)
          }}</template>
        </el-table-column>
        <el-table-column sortable label="状态" prop="enableState">
          <template>
            <el-switch
              v-model="value"
              active-color="#13ce66"
              inactive-color="#ff4949"
            />
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              @click="handleClick(scope.row)"
            >查看</el-button>
            <el-button type="text" size="small">编辑</el-button>
            <el-button type="text" size="small">编辑</el-button>
            <el-button type="text" size="small">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div style="height: 60px; margin-top: 10px; text-align: right">
        <el-pagination
          :page-sizes="[3, 5, 10]"
          :page-size="size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
    <!-- 新增员工 dialog -->
    <el-dialog title="收货地址" :visible.sync="dialogFormVisible">
      <empDia />
    </el-dialog>
  </div>
</template>

<script>
import empDia from './empDia.vue'
import Employment from '../../constant/employees'
import undeatImg from '@/assets/common/img.jpeg'
import { getEmployeeList } from '../../api/employees'
import { formatDate } from '@/utils'
export default {
  name: '',
  components: {
    empDia
  },
  data() {
    return {
      undeatImg,
      value: true,
      list: [
        {
          date: '2016-05-02',
          name: '王小虎',
          province: '上海',
          city: '普陀区',
          address: '上海市普陀区金沙江路 1518 弄',
          zip: 200333
        },
        {
          date: '2016-05-04',
          name: '王小虎',
          province: '上海',
          city: '普陀区',
          address: '上海市普陀区金沙江路 1517 弄',
          zip: 200333
        },
        {
          date: '2016-05-01',
          name: '王小虎',
          province: '上海',
          city: '普陀区',
          address: '上海市普陀区金沙江路 1519 弄',
          zip: 200333
        },
        {
          date: '2016-05-03',
          name: '王小虎',
          province: '上海',
          city: '普陀区',
          address: '上海市普陀区金沙江路 1516 弄',
          zip: 200333
        }
      ],
      page: 1, // 查询第一页
      size: 3, // 每页两条
      index: 1,
      total: 0,
      dialogFormVisible: false
    }
  },
  computed: {},
  created() {
    this.loadEmployeeList()
  },
  methods: {
    handleClick(row) {
      console.log(row)
    },
    async loadEmployeeList() {
      const res = await getEmployeeList(this.page, this.size).catch(() =>
        this.$message.error('加载员工数据失败')
      )
      console.log(res)
      this.list = res.data.data.rows
      this.total = res.data.data.total
    },
    parseDate(date) {
      return formatDate(date)
    },
    handleSizeChange(val) {
      this.index = 1
      this.size = val
      this.index += val * (this.page - 1)
      this.loadEmployeeList()
      // console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      this.index = 1
      this.page = val
      this.index += (val - 1) * this.size
      this.loadEmployeeList()
      // console.log(`当前页: ${val}`);
    },
    handleEmployment(num) {
      let objEmployment = ''
      Employment.hireType.forEach((ele) => {
        if (ele.id == num) {
          objEmployment = ele.value
        }
      })
      return objEmployment
    },
    addEmpDia () {
      this.dialogFormVisible=true
    }
  }
}
</script>
<style lang="scss" scoped>
.pageTools {
  padding: 20px;
  .el-card {
    margin-top: 10px;
  }
}
</style>
